/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jdesktop.wonderland.modules.hslutemplatecell.client;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.wonderland.modules.hslutemplatecell.client.jme.cellrenderer.TemplateCellRenderer;
import org.jdesktop.wonderland.common.cell.state.CellClientState;
import org.jdesktop.wonderland.client.cell.Cell;
import org.jdesktop.wonderland.client.cell.Cell.RendererType;
import org.jdesktop.wonderland.client.cell.CellCache;
import org.jdesktop.wonderland.client.cell.CellRenderer;
import org.jdesktop.wonderland.client.cell.ChannelComponent;
import org.jdesktop.wonderland.client.cell.ChannelComponent.ComponentMessageReceiver;
import org.jdesktop.wonderland.client.input.Event;
import org.jdesktop.wonderland.client.input.EventClassListener;
import org.jdesktop.wonderland.client.jme.input.MouseButtonEvent3D;
import org.jdesktop.wonderland.client.jme.input.MouseEvent3D;
import org.jdesktop.wonderland.common.cell.CellID;
import org.jdesktop.wonderland.modules.hslutemplatecell.common.TemplateCellClientState;
import org.jdesktop.wonderland.common.cell.CellStatus;
import org.jdesktop.wonderland.common.cell.messages.CellMessage;
import org.jdesktop.wonderland.modules.hslutemplatecell.common.TemplateCellChangeMessage;
import org.jdesktop.wonderland.modules.hslutemplatecell.common.TemplateCellGetShapeMessage;

/**
 *
 * @author Dominik
 */
public class TemplateCell extends Cell {

    private String shapeType = null;
    private TemplateCellRenderer renderer = null;
    private PollingShape pollingThread = null;
    private boolean dirty = false;

    public TemplateCell(CellID cellID, CellCache cellCache) {
        super(cellID, cellCache);
        pollingThread = new PollingShape();
        pollingThread.start();
    }

    @Override
    public void setClientState(CellClientState state) {
        super.setClientState(state);
        this.shapeType = ((TemplateCellClientState) state).getShapeType();
        if(renderer != null)
            renderer.updateShape();
    }

    public String getShapeType() {
        return this.shapeType;
    }

    @Override
    protected CellRenderer createCellRenderer(RendererType rendererType) {
        if (rendererType == RendererType.RENDERER_JME) {
            this.renderer = new TemplateCellRenderer(this);
            return this.renderer;
        } else {
            return super.createCellRenderer(rendererType);
        }
    }

    @Override
    public void setStatus(CellStatus status, boolean increasing) {
        super.setStatus(status, increasing);
        if (status == CellStatus.INACTIVE && increasing == false) {
            ChannelComponent channel = getComponent(ChannelComponent.class);
            channel.removeMessageReceiver(TemplateCellGetShapeMessage.class);
        } else if (status == CellStatus.RENDERING && increasing == true) {
            ChannelComponent channel = getComponent(ChannelComponent.class);
            channel.addMessageReceiver(TemplateCellGetShapeMessage.class, new ShapeCellMessageReceiver());
        }
    }

    class MouseEventListener extends EventClassListener {

        @Override
        public Class[] eventClassesToConsume() {
            return new Class[]{MouseButtonEvent3D.class};
        }
        // Note: we don't override computeEvent because we don't do any computation in this listener.

        @Override
        public void commitEvent(Event event) {
            MouseButtonEvent3D mbe = (MouseButtonEvent3D) event;
            if (mbe.isClicked() == false || mbe.getButton() != MouseEvent3D.ButtonId.BUTTON1) {
                return;
            }
            shapeType = (shapeType.equals("SUMMER") == true) ? "WINTER" : "SUMMER";
            renderer.updateShape();
            TemplateCellChangeMessage msg = new TemplateCellChangeMessage(getCellID(), shapeType);
            sendCellMessage(msg);
        }
    }

    class ShapeCellMessageReceiver implements ComponentMessageReceiver {

        public void messageReceived(CellMessage message) {
            if (message instanceof TemplateCellChangeMessage) {
                TemplateCellChangeMessage sccm = (TemplateCellChangeMessage) message;
                if (!sccm.getSenderID().equals(getCellCache().getSession().getID())) {
                    shapeType = sccm.getShapeType();
                    renderer.updateShape();
                }
            } else if (message instanceof TemplateCellGetShapeMessage) {
                TemplateCellGetShapeMessage sccm = (TemplateCellGetShapeMessage) message;
                
                if (!shapeType.equals(sccm.getShapeType())) {
                    shapeType = sccm.getShapeType();
                    renderer.updateShape();
                    dirty = false;
                }
            }
        }
    }

    private class PollingShape extends Thread {

        private boolean running = true;

        @Override
        public void run() {

            while (isRunning()) {
                TemplateCellGetShapeMessage msg = new TemplateCellGetShapeMessage(getCellID(), null);
                sendCellMessage(msg);

                try {
                    Thread.sleep(30000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(TemplateCell.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }

        /**
         * @return the running
         */
        public boolean isRunning() {
            return running;
        }

        /**
         * @param running the running to set
         */
        public void setRunning(boolean running) {
            this.running = running;
        }
    }
}
